import { _decorator, Animation, AnimationState, AudioClip, AudioSource, Collider2D, Component, Contact2DType, find, IPhysics2DContact, Node, RigidBody2D, v2 } from 'cc';
import { Monster } from './Monster';
import { MainLlogic } from './MainLlogic';
const { ccclass, property } = _decorator;

@ccclass('Missile')
export class Missile extends Monster {

    /**
     * 警告
     */
    @property({type:AudioClip})
    private jgclip:AudioClip = null
    /**
     * 飞行
     */
    @property({type:AudioClip})
    private flyclip:AudioClip = null
    /**
     * 爆炸
     */
    @property({type:AudioClip})
    private bombclip:AudioClip = null


    private player:Node = null

    countdown:number = 60

    launch:boolean = false

    rigidbody:RigidBody2D = null

    boom:boolean = false

    //missile_1_fly,missile_boom
    anim ='missile_1_fly'

    ani:Animation = null
    missileAS:AudioSource = null
    playjg:boolean = true
    start() {
        this.player = find('Canvas/MainLogic/Player')
        this.rigidbody = this.node.getComponent(RigidBody2D)
        this.ani = this.node.getComponent(Animation)
        this.ani.on(Animation.EventType.FINISHED,this.onAnimationEvent,this)
        this.node.getComponent(Collider2D).on(Contact2DType.BEGIN_CONTACT,this.onBeginContact,this)
        this.missileAS = this.node.getComponent(AudioSource)
    }

    onAnimationEvent(type: Animation.EventType, state: AnimationState) {
        if(state.name == 'missile_boom'){
            this.recovery()
        }
        
    }

    setani(anim){
        if(this.anim == anim) return
        this.anim = anim
        this.ani.play(anim)
    }
    //发射
    missileLanuch(){
        this.launch = true
        if(this.anim == 'missile_1_fly') this.rigidbody.linearVelocity = v2(-100,0)
    }

    /**
     * 怪物节点初始化
     * @param monsterpoolnum 节点池编号
     */
    initialize(monsterpoolnum: number): void {
        this.rigidbody = this.node.getComponent(RigidBody2D)
        this.player = find('Canvas/MainLogic/Player')
        this.node.setPosition(this.node.position.x,this.player.position.y,0)
        this.rigidbody.linearVelocity = v2(0,0)
        this.setani('missile_1_fly')
        this.monsterpoolnum = monsterpoolnum
        this.countdown = 60
        this.launch = false
        this.playjg = true
        this.node.getParent().getComponent(MainLlogic).createjg(this.node.position.y)
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact): void {
        if(otherCollider.group == 2){
            this.setani('missile_boom')
            this.missileAS.playOneShot(this.bombclip)
            this.rigidbody.linearVelocity = v2(-20,0)
        }
    }

    update(deltaTime: number) {
        if(this.countdown == 0){
            if(!this.launch){
                this.missileLanuch()
            }
        }else{
            this.countdown--
            this.rigidbody.linearVelocity = v2(this.player.getComponent(RigidBody2D).linearVelocity.x*0.9,0)
        }
        if(this.node.position.x -  this.player.position.x < -2000){
            this.recovery()
        }
        if(this.playjg){
            this.playjg = false
            this.missileAS.playOneShot(this.jgclip)
        }
        if(this.countdown == 20){
            this.missileAS.playOneShot(this.flyclip)
        }
    }
}


